<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            router
        >
            <template v-for="item in menus">
                <template v-if="item.children">
                    <el-submenu :index="item.id + ''" :key="item.id">
                        <template slot="title">
                            <i :class="item.id | iconClass"></i>
                            <span slot="title">{{ item.name }}</span>
                        </template>
                        <template v-for="subItem in item.children">
                            <el-submenu v-if="subItem.children" :index="subItem.route" :key="subItem.id">
                                <template slot="title">{{ subItem.name }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.children" :key="i" :index="threeItem.route">{{
                                    threeItem.name
                                }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.route" :key="subItem.id">{{ subItem.name }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.id + ''" :key="item.id">
                        <i :class="item.id | iconClass"></i>
                        <span slot="title">{{ item.name }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
export default {
    props: {
        menus: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            collapse: false,
            routes: [
                {
                    id: 1,
                    pId: 0,
                    name: '账号管理',
                    type: 0,
                    checked: 1,
                    children: [
                        {
                            id: 6,
                            pId: 1,
                            name: '系统账号',
                            url: '/adminUser/**',
                            type: 1,
                            checked: 1,
                            route: 'account'
                        },
                        {
                            id: 7,
                            pId: 1,
                            name: '角色管理',
                            url: '/role/**',
                            type: 1,
                            checked: 1,
                            route: 'roleManage'
                        }
                    ]
                },
                {
                    id: 2,
                    pId: 0,
                    name: '用户管理',
                    type: 0,
                    checked: 1,
                    children: [
                        {
                            id: 8,
                            pId: 2,
                            name: '用户管理',
                            url: '/otherPlateUser/**',
                            type: 1,
                            checked: 1,
                            route: 'userManage'
                        },
                        {
                            id: 9,
                            pId: 2,
                            name: '设备管理',
                            url: '/device/**',
                            type: 1,
                            checked: 1,
                            route: 'deviceManage'
                        }
                    ]
                },
                {
                    id: 3,
                    pId: 0,
                    name: 'Banner管理',
                    type: 0,
                    checked: 1,
                    children: [
                        {
                            id: 10,
                            pId: 3,
                            name: '睡眠报告Banner',
                            url: '/article/**',
                            type: 1,
                            checked: 1,
                            route: 'reportBanner'
                        }
                    ]
                },
                {
                    id: 4,
                    pId: 0,
                    name: '推送管理',
                    type: 0,
                    checked: 1,
                    children: [
                        {
                            id: 11,
                            pId: 4,
                            name: '消息推送',
                            url: '/systemMsg/**',
                            type: 1,
                            checked: 1,
                            route: 'msgPush'
                        }
                    ]
                },
                {
                    id: 5,
                    pId: 0,
                    name: '客服',
                    type: 0,
                    checked: 1,
                    children: [
                        {
                            id: 12,
                            pId: 5,
                            name: '用户反馈',
                            url: '/feedback/**',
                            type: 1,
                            checked: 1,
                            route: 'feedBack'
                        }
                    ]
                }
            ],

            items: [
                {
                    icon: 'el-icon-lx-addressbook',
                    index: '0',
                    title: '系统账号管理',
                    id: '1',
                    checked: 0,
                    subs: [
                        {
                            index: 'account',
                            title: '系统账号',
                            id: '6',
                            checked: 0
                        },
                        {
                            index: 'roleManage',
                            title: '角色管理',
                            id: '7',
                            checked: 0
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-vipcard',
                    index: '1',
                    title: '用户管理',
                    id: '2',
                    checked: 0,
                    subs: [
                        {
                            index: 'userManage',
                            title: '用户管理',
                            id: '8',
                            checked: 0
                        },
                        {
                            index: 'deviceManage',
                            title: '设备管理',
                            id: '9',
                            checked: 0
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-pic',
                    index: '2',
                    title: 'Banner管理',
                    id: '3',
                    checked: 0,
                    subs: [
                        // {
                        //     index: 'homeBanner',
                        //     title: '首页Banner',
                        //     id: '10',
                        //     checked: 0
                        // },
                        {
                            index: 'reportBanner',
                            title: '睡眠报告Banner',
                            id: '10',
                            checked: 0
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-comment',
                    index: '3',
                    title: '推送管理',
                    id: '1',
                    checked: 0,
                    subs: [
                        {
                            index: 'msgPush',
                            title: '消息推送',
                            id: '1',
                            checked: 0
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-service',
                    index: '4',
                    title: '客服',
                    id: '1',
                    checked: false,
                    subs: [
                        {
                            index: 'feedBack',
                            title: '用户反馈',
                            id: '1',
                            checked: false
                        }
                    ]
                },
                // {
                //     icon: 'el-icon-lx-home',
                //     index: 'dashboard',
                //     title: '系统首页'
                // },
                {
                    icon: 'el-icon-lx-cascades',
                    index: 'table',
                    title: '基础表格'
                },
                {
                    icon: 'el-icon-lx-copy',
                    index: 'tabs',
                    title: 'tab选项卡'
                },
                {
                    icon: 'el-icon-lx-calendar',
                    index: '3',
                    title: '表单相关',
                    subs: [
                        {
                            index: 'form',
                            title: '基本表单'
                        },
                        {
                            index: '3-2',
                            title: '三级菜单',
                            subs: [
                                {
                                    index: 'editor',
                                    title: '富文本编辑器'
                                },
                                {
                                    index: 'markdown',
                                    title: 'markdown编辑器'
                                }
                            ]
                        },
                        {
                            index: 'upload',
                            title: '文件上传'
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-emoji',
                    index: 'icon',
                    title: '自定义图标'
                },
                {
                    icon: 'el-icon-pie-chart',
                    index: 'charts',
                    title: 'schart图表'
                },
                {
                    icon: 'el-icon-rank',
                    index: '6',
                    title: '拖拽组件',
                    subs: [
                        {
                            index: 'drag',
                            title: '拖拽列表'
                        },
                        {
                            index: 'dialog',
                            title: '拖拽弹框'
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-global',
                    index: 'i18n',
                    title: '国际化功能'
                },
                {
                    icon: 'el-icon-lx-warn',
                    index: '7',
                    title: '错误处理',
                    subs: [
                        {
                            index: 'permission',
                            title: '权限测试'
                        },
                        {
                            index: '404',
                            title: '404页面'
                        }
                    ]
                },
                {
                    icon: 'el-icon-lx-redpacket_fill',
                    index: '/donate',
                    title: '支持作者'
                }
            ]
        };
    },
    filters: {
        iconClass(id) {
            if (id == 1) {
                return 'el-icon-lx-addressbook';
            } else if (id == 2) {
                return 'el-icon-lx-vipcard';
            } else if (id == 3) {
                return 'el-icon-lx-pic';
            } else if (id == 4) {
                return 'el-icon-lx-comment';
            } else if (id == 5) {
                return 'el-icon-lx-service';
            } else if (id == 14) {
                return 'el-icon-lx-sort';
            }
        }
    },
    computed: {
        // onRoutes() {
        //     console.log('🚀 ~ file: Sidebar.vue ~ line 373 ~ onRoutes ~ this.menus', this.menus);
        //     return this.menus[0].children[0].route;
        // }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
